package com.ruoyi.human.controller;

import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.human.domain.*;
import com.ruoyi.system.api.RemoteProjectResearchService;
import com.ruoyi.system.api.domain.ProjectResearch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.human.service.IHumanClinicalTrialService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 人体临床实验基本信息Controller
 * 
 * @author gw
 * @date 2025-01-06
 */
@RestController
@RequestMapping("/trial")
public class HumanClinicalTrialController extends BaseController
{
    @Autowired
    private IHumanClinicalTrialService humanClinicalTrialService;

    @Autowired
    private RemoteProjectResearchService remoteProjectResearchService;
    /**
     * 查询人体临床实验基本信息列表
     */
    @RequiresPermissions("human:trial:list")
    @GetMapping("/list")
    public TableDataInfo list(HumanClinicalTrial humanClinicalTrial)
    {
        startPage();
        List<HumanClinicalTrial> list = humanClinicalTrialService.selectHumanClinicalTrialList(humanClinicalTrial);
        for (HumanClinicalTrial humanClinicalTrial1:list){
            Long projectId=humanClinicalTrial1.getProjectId();
            ProjectResearch projectResearch=remoteProjectResearchService.getProjectInfo(projectId).getData();
            if(projectResearch!=null){
                humanClinicalTrial1.setProjectName(projectResearch.getProjectName());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出人体临床实验基本信息列表
     */
    @RequiresPermissions("human:trial:export")
    @Log(title = "人体临床实验基本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HumanClinicalTrial humanClinicalTrial)
    {
        List<HumanClinicalTrial> list = humanClinicalTrialService.selectHumanClinicalTrialList(humanClinicalTrial);
        ExcelUtil<HumanClinicalTrial> util = new ExcelUtil<HumanClinicalTrial>(HumanClinicalTrial.class);
        util.exportExcel(response, list, "人体临床实验基本信息数据");
    }

    /**
     * 获取人体临床实验基本信息详细信息
     */
    @RequiresPermissions("human:trial:query")
    @GetMapping(value = "/{trialId}")
    public AjaxResult getInfo(@PathVariable("trialId") Long trialId)
    {
        return success(humanClinicalTrialService.selectHumanClinicalTrialByTrialId(trialId));
    }

    /**
     * 获取人体临床实验基本信息详细信息,网关
     */
    @RequiresPermissions("human:trial:query")
    @GetMapping(value = "/trial/{trialId}")
    public R<HumanClinicalTrial> getTrialInfo(@PathVariable("trialId") Long trialId)
    {
        return R.ok(humanClinicalTrialService.selectHumanClinicalTrialByTrialId(trialId));
    }

    /**
     * 新增人体临床实验基本信息
     */
    @RequiresPermissions("human:trial:add")
    @Log(title = "人体临床实验基本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HumanClinicalTrial humanClinicalTrial)
    {
        humanClinicalTrial.setCreatedAt(new Date());
        humanClinicalTrial.setCreatedBy(SecurityUtils.getUsername());
        return toAjax(humanClinicalTrialService.insertHumanClinicalTrial(humanClinicalTrial));
    }

    /**
     * 修改人体临床实验基本信息
     */
    @RequiresPermissions("human:trial:edit")
    @Log(title = "人体临床实验基本信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HumanClinicalTrial humanClinicalTrial)
    {
        humanClinicalTrial.setUpdatedBy(SecurityUtils.getUsername());
        humanClinicalTrial.setUpdatedAt(new Date());
        humanClinicalTrial.setVersion(humanClinicalTrial.getVersion()+1);
        return toAjax(humanClinicalTrialService.updateHumanClinicalTrial(humanClinicalTrial));
    }

    /**
     * 删除人体临床实验基本信息
     */
    @RequiresPermissions("human:trial:remove")
    @Log(title = "人体临床实验基本信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{trialIds}")
    public AjaxResult remove(@PathVariable Long[] trialIds)
    {
        for (Long id:trialIds){
            HumanClinicalTrial humanClinicalTrial=humanClinicalTrialService.selectHumanClinicalTrialByTrialId(id);
            humanClinicalTrial.setDeletedAt(new Date());
            humanClinicalTrial.setDeletedBy(SecurityUtils.getUsername());
            humanClinicalTrial.setDeletedFlag(1);
            humanClinicalTrialService.updateHumanClinicalTrial(humanClinicalTrial);
        }
        return toAjax(true);
    }
    @GetMapping("/getTrials")
    public AjaxResult getTrials()
    {
        HumanClinicalTrial trialQuery = new HumanClinicalTrial();
        trialQuery.setDeletedFlag(0);
        List<HumanClinicalTrial> list = humanClinicalTrialService.selectHumanClinicalTrialList(trialQuery);
        return success(list);
    }
}
